package com.zy.design.observer.v2;

import com.zy.design.observer.v1.Customer;

import org.apache.log4j.Logger;

import java.util.HashSet;
import java.util.Set;

/**
 * 促销管理被观察者，  观察者模式
 * @author hugx
 * @create 2018-09-30 10:07
 */
public class PromotionObserver {

    private static final Logger log=Logger.getLogger(PromotionObserver.class);

    private Set<Customer> customers=new HashSet<>();

    /**
     * 促销活动
     */
    public void activity(String msg){
        log.info("**** 开始  "+msg+"  促销活动  ****");
        notification(msg);

    }

    /**
     * 注册客户
     * @param customer
     */
    public void regeditCustomer(Customer customer){
        if(customer == null){
            return;
        }

        customers.add(customer);
    }

    /**
     * 注销客户
     * @param customer
     */
    public void cancelCustomer(Customer customer){
        if(customer == null ){
            return ;
        }
        customers.remove(customer);
    }

    /**
     * 通知观察者关注促销活动
     * @param msg
     */
    private void notification(String msg){
        if(null == msg || "".equals(msg)){
            return ;
        }

        for(Customer customer: customers){
            customer.acceptInfo(msg);
        }
    }
}
